//
//  Router.m
//  YZ
//

#import "YZRouter.h"
#import "AppDelegate.h"
#import "MJExtension.h"

@interface YZRouter()

@end

@implementation YZRouter

SingletonM

#pragma mark - public function

- (void)showActionSheet:(UIViewController*)sheet {
 
    [self showViewController:sheet method:RouterMethodPresentAnimate];
}

#pragma mark -

/*********** push 转场 ***********/

- (void)pushSB:(NSString*)sbName withIdentifier:(NSString*)identifier {
    
    UIStoryboard *launchSB = [UIStoryboard storyboardWithName:sbName bundle:nil];
    UIViewController *vc = [launchSB instantiateViewControllerWithIdentifier:identifier];
    [self showViewController:vc method:RouterMethodPushAnimate];
}

- (void)pushNib:(NSString*)vcName {
    
    [self pushNib:vcName withParams:nil];
}

- (void)pushNib:(NSString*)vcName withParams:(NSDictionary*)params {
    
    UIViewController *vc = [self loadVCName:vcName withNib:YES];
    if (params) [vc mj_setKeyValues:params];
    [self showViewController:vc method:RouterMethodPushAnimate];
}

- (void)pushVCName:(NSString*)vcName {
    
    UIViewController *vc = [self loadVCName:vcName withNib:NO];
    [self showViewController:vc method:RouterMethodPushAnimate];
}

- (void)pushVC:(UIViewController*)vc {
    
    [self showViewController:vc method:RouterMethodPushAnimate];
}

/*********** push 转场 ***********/


#pragma mark -

/*********** present 转场 ***********/

- (void)presentSB:(NSString*)sbName withIdentifier:(NSString*)identifier {
    
    UIStoryboard *launchSB = [UIStoryboard storyboardWithName:sbName bundle:nil];
    UIViewController *vc = [launchSB instantiateViewControllerWithIdentifier:identifier];
    [self showViewController:vc method:RouterMethodPresentAnimate];
}

- (void)presentNib:(NSString*)vcName {
    
    UIViewController *vc = [self loadVCName:vcName withNib:YES];
    [self showViewController:vc method:RouterMethodPresentAnimate];
}

- (void)presentVCName:(NSString*)vcName {
    
    UIViewController *vc = [self loadVCName:vcName withNib:NO];
    [self showViewController:vc method:RouterMethodPresentAnimate];
}

- (void)presentVC:(UIViewController *)vc {
    
    [self showViewController:vc method:RouterMethodPresentAnimate];
}

/*********** present 转场 ***********/

#pragma mark -

/*********** common function ***********/

- (void)showViewController:(UIViewController*)vc method:(RouterMethod)method {
 
    NSAssert(vc, @"没有找到要弹出的 vc");
    
    UIViewController *currentVC = [self currentViewController];
    if (currentVC) {
            switch (method) {
                    
                // Push
                case RouterMethodPush:
                {
                    if (currentVC.navigationController) {
                     
                        [currentVC.navigationController pushViewController:vc animated:NO];
                    }
                }
                    break;
                    
                case RouterMethodPushAnimate:
                {
                    if (currentVC.navigationController) {
                        
                        [currentVC.navigationController pushViewController:vc animated:YES];
                    }
                }
                    break;
                    
                    
                // Present
                case RouterMethodPresent:
                {
                    [currentVC presentViewController:vc animated:NO completion:^{}];
                }
                    break;
                    
                case RouterMethodPresentAnimate:
                {
                    [currentVC presentViewController:vc animated:YES completion:^{}];
                }
                    break;
        }
    }
}

- (UIViewController*)loadVCName:(NSString*)vcName withNib:(BOOL)isNib {
    
    if (isNib) {
        
        return [[NSClassFromString(vcName) alloc] initWithNibName:vcName bundle:nil];
    } else {
     
        return [[NSClassFromString(vcName) alloc] init];
    }
}

/*********** common function ***********/




#pragma mark - private function

- (UIViewController *)currentViewController {
    
    UIViewController *result = nil;
    
    result = [self getCurrentVCFrom:self.rootWindow.rootViewController];

    
    NSAssert(result, @"检查 为啥没有根 vc");
    
    return result;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC {
    
    UIViewController *currentVC = nil;
    if ([rootVC presentedViewController]) {
        
        // 视图是被 presented 出来的
        [[rootVC presentedViewController] dismissViewControllerAnimated:YES completion:nil];
        if ([rootVC isKindOfClass:[UINavigationController class]]) {
            
            currentVC = [(UINavigationController *)rootVC topViewController];
        }
        
        if ([rootVC isKindOfClass:[UITabBarController class]]) {
            
            currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        }
        
    } else if ([rootVC isKindOfClass:[UITabBarController class]]) {
        
        // 根视图为 UITabBarController
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        
        // 根视图为 UINavigationController
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
       
        // 根视图为非导航类
        currentVC = rootVC;
    }
    
    return currentVC;
}

#pragma mark - getter

- (UIWindow *)rootWindow {
    
    UIWindow *result = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).window;
    
    NSAssert(result, @"找不到 root window");
    
    return result;
}





//- (void) routeURL:(NSString *)urlString params:(NSDictionary*)params {
//
////    UINavigationController *nav = [self rootNavigationController];
////    if ([[params allKeys] containsObject:kJumpVC]) {
////
////        NSInteger jumpSize = [params[kJumpVC] integerValue];
////        UIWindow *window = [UIApplication sharedApplication].keyWindow;
////        UIViewController *presentVC = [[URLRouter sharedInstance] getCurrentVCFrom:window.rootViewController];
////        if (jumpSize == 1 && [presentVC presentingViewController]) {
////
////            [presentVC dismissViewControllerAnimated:YES completion:nil];
////        }
////        // 传递参数
////        [vc mj_setKeyValues:params];
////        NSInteger navVCSize = nav.childViewControllers.count;
////        if (jumpSize > 0 && jumpSize < navVCSize) {
////
////            NSMutableArray* navArray = [[NSMutableArray alloc] initWithArray:nav.viewControllers];
////            while (jumpSize-- > 0) {
////
////                [navArray removeLastObject];
////            }
////            [navArray addObject:vc];
////            [nav setViewControllers:navArray animated:YES];
////        } else {
////
////            [nav pushViewController:vc animated:YES];
////        }
////    } else {
////
////        [nav pushViewController:vc animated:YES];
////    }
//}


@end
